package com.kevinwu.distributedsss.controller;

import com.kevinwu.distributedsss.pojo.LoginUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录控制器类 实现登录首页跳转 登录 以及 注销
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    /**
     * 登录主界面跳转
     * @return
     */
    @RequestMapping("/loginindex")
    public ModelAndView goLogin(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return  mv;
    }

    /**
     * 执行登录登录成功后session中放入用户名 登录失败跳转到登录页面
     * @param request
     * @param response
     * @param loginUser
     * @return
     */
    @RequestMapping("/dologin")
    public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response, LoginUser loginUser,HttpSession session){
        if("admin".equals(loginUser.getUserId()) && "admin".equals(loginUser.getPassWd())){
//            HttpSession session = request.getSession();
            session.setAttribute("username",loginUser.getUserId());
            String url = "redirect:/resume/list";
            ModelAndView mav = new ModelAndView(url);
            return  mav;
        }else{
            ModelAndView mv = new ModelAndView();
            mv.setViewName("login");
            return  mv;
        }
    }

    /**
     * 实现session的注销
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession(false);
        session.invalidate();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return  mv;
    }
}
